<template>
  <el-dropdown class="user-avatar-wrapper" @command="handleCommand">
    <div class="avatar-box">
      <span class="username">{{ userInfo.username }}</span>
      <i class="el-icon-caret-bottom" />
    </div>

    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item command="userCenter">个人中心</el-dropdown-item>
        <el-dropdown-item command="loginOut">退出登录</el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<script setup>
import { inject, ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessageBox, ElMessage } from 'element-plus'

// 定义组件名称
defineOptions({
  name: 'UserAvatar'
})

// 注入用户信息
const userInfo = inject('userInfo')
const router = useRouter()

// 定义响应式数据
const imgBaseUrl = ref('')

// 处理命令
const handleCommand = (command) => {
  if (command === 'userCenter') {
    router.push({ path: '/personInfo' })
  }
  if (command === 'loginOut') {
    loginOut()
  }
}

// 退出登录
const loginOut = () => {
  ElMessageBox.confirm('确定注销并退出系统吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    localStorage.removeItem("backUser")
    router.push({ path: '/login' })
  }).catch(() => {
    // 用户取消操作
  })
}

// 生命周期钩子
onMounted(() => {
  console.log(userInfo)
})
</script>

<style lang="less">
.user-avatar-wrapper {
  float: left;
  // width: 120px;
  padding: 3px 0 3px 20px;
  margin-left: 20px;
  border-left: solid 1px #ddd;
  cursor: pointer;

  .avatar-box {
    outline: none;
    display: flex;
    align-items: center; /* 垂直居中对齐 */
    justify-content: space-between; /* 头像和下拉箭头之间留有空间 */
  }

  .username {
    font-size: 14px;
    /* 根据需要调整字体大小 */
    color: #333;
    /* 字体颜色 */
    margin-left: 5px;
    /* 与头像之间的间距 */
  }

  .el-avatar--small {
    display: inline-block;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }

  i {
    display: inline-block;
    vertical-align: middle;
    margin-left: 2px;
  }
}
</style>